package com.prs.crm.service.hr.impl;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.AbstractDataset;
import org.jfree.data.general.DefaultPieDataset;

import com.prs.crm.context.Attach;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.FileUtil;
import com.prs.crm.context.ToolKits;
import com.prs.crm.dao.hr.LeaveDao;
import com.prs.crm.domain.hr.Leave;
import com.prs.crm.domain.hr.LeaveType;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.hr.LeaveService;
import com.prs.crm.service.impl.BaseServiceImpl;

public class LeaveServiceImpl extends BaseServiceImpl<Leave,LeaveDao> implements LeaveService{

	@Override
	public void saveLeave(Leave leave, File file, String fileName) {
		// TODO Auto-generated method stub
	//	Attach attach = FileUtil.getInstance().saveFile(file, fileName, "", Constrains.UPLOAD_DIC_LEAVE);
		//if(attach.getSrc() != null) {
			//leave.setAttach(attach.getSrc());
		//}
		leave.setAttach(FileUtil.getInstance().createAttach(file, fileName));
		this.getDao().save(leave);
	}

	@Override
	public List<LeaveType> getAllLeaveType() {
		return this.getDao().getAllLeaveType();
	}

	@Override
	public Collection<Leave> getAllLeave() {
		// TODO Auto-generated method stub
		return this.getDao().getAllLeave();
	}
	@Override
	public Collection<Object[]> findByType(LeaveType type, Date startTime, Date endTime, Collection<Person> persons)
	{
		return this.getDao().findByType(type, startTime, endTime, persons);
	}

	@Override
	public Collection<Leave> findByPerson(Person person) {
		return this.getDao().findByPerson(person);
	}


	@Override
	public Collection<Leave> getLeaveByIds(Integer[] ids) {
		// TODO Auto-generated method stub
		return this.getDao().getLeaveByIds(ToolKits.convertToIntAry(ids));
	}

	public Collection<Person> convertIdsToPersons(Collection<Integer> ids){
		Collection<Person> persons = new ArrayList<Person>();
		for(Integer id : ids){
			Person person = new Person();
			person.setId(id);
			persons.add(person);
		}
		return persons;
	}
	public AbstractDataset fillPieDataset(Collection<Object[]> leaveList){
		DefaultPieDataset pieDataset = new DefaultPieDataset();
		for(Object[] o : leaveList){
			String name = "";
			Integer count = 0;
			Integer id = 0;
			for(Object value : o){
				if(value != null){
					if(value instanceof Person){
						name = ((Person) value).getResume().getName();
						id = ((Person) value).getId();
					}else if(value instanceof Integer){
						count = (Integer)value;
					}
				}
			}
			if(!name.equals(""))
				pieDataset.setValue(name+"["+id+"]", count);
		}
		return pieDataset;
	}
	public AbstractDataset fillBarDataset(Collection<Object[]> leaveList){
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();
		for(Object[] o : leaveList){
			String name = "";
			Integer count = 0;
			Integer id = 0;
			for(Object value : o){
				if(value != null){
					if(value instanceof Person){
						name = ((Person) value).getResume().getName();
						id = ((Person) value).getId();
					}else if(value instanceof Integer){
						count = (Integer)value;
					}
				}
			}
			if(!name.equals(""))
				dataset.addValue(count, name, name+"["+id+"]");
		}
		return dataset;
	}

	@Override
	public Person getPerson(String series) {
		Person person = new Person();
		if(series.indexOf("[") != -1 && series.indexOf("]") != -1)
			person.setId(Integer.parseInt(series.substring(series.indexOf("[")+1, series.indexOf("]"))));
		else
			person.setId(0);
		return person;
	}

	@Override
	public DetachedCriteria findByPage(DetachedCriteria criteria,
			LeaveType type, Date startTime, Date endTime, Person person) {
		if (type!=null)
    	{
    		criteria.add(Restrictions.eq("type", type));
    	}
    	if (startTime!=null)
    	{
    		criteria.add(Restrictions.ge("beginTime", startTime));
    	}
    	if (endTime!=null)
    	{
    		criteria.add(Restrictions.le("beginTime", endTime));
    	}
    	if (person!=null)
    	{
    		criteria.add(Restrictions.eq("recorder", person));
    	}
    	criteria.add(Restrictions.eq("state",Constrains.TASK_PASS));
		return criteria;
	}

	@Override
	public Collection<Leave> getLeavesByPersonInMonth(Person person,
			Integer year, Integer month) {
		// TODO Auto-generated method stub
		return this.getDao().getLeavesByPersonInMonth(person, year, month);
	}

	@Override
	public void saveLeaves(Leave leave, File file, String fileName) {
		// TODO Auto-generated method stub
		Attach lAttach=FileUtil.getInstance().saveFile(file, fileName, "", Constrains.UPLOAD_DIC_LEAVE);
		if(lAttach.getSrc()!=null){
			leave.setLeave_file(lAttach.getSrc());
		}
		this.getDao().save(leave);
	}
}
